#include "myhash.h"
#include "prime.h"
#include <iostream>
using namespace std;
void SepHash::insert(int x)
{
	cout << "insert" << x << endl;

	int pos = myhash(x);
	
	list<int> & whichlist = m_septable[pos];

	for (list<int>::iterator itr = whichlist.begin(); itr != whichlist.end(); ++itr)
	{
		if (*itr == x)
			return;
	}

	whichlist.push_front(x);
	currsize++;

	if (currsize == tablesize)
		rehash();

}

bool SepHash::remove(int x)
{
	cout << "remove" << x  << endl;
	int pos = myhash(x);
	list<int> & whichlist = m_septable[pos];
	bool hasx = false;

	for (list<int>::iterator itr = whichlist.begin(); itr != whichlist.end(); ++itr)
	{
		if (*itr == x)
		{
			hasx = true;
			whichlist.erase(itr);
			--currsize;
			break;
		}
	}


	return hasx;
	


}


void SepHash::rehash()
{

	cout << "rehash" << endl;
	vector<list<int>> old = m_septable;

	int thesize = nextPrime(tablesize * 2);

	m_septable = vector<list<int>>(thesize);

	for (int i = 0; i < old.size(); ++i)
	{
		list<int> &whichlist = old[i];
		for (list<int>::iterator itr = whichlist.begin(); itr != whichlist.end(); ++itr)
		{
			insert(*itr);
		}

	}

}

void SepHash::desc()
{
	for (int i = 0; i < m_septable.size(); ++i)
	{
		list<int> &whichlist = m_septable[i];
		cout << "position " << i << ": ";
		for (list<int>::iterator itr = whichlist.begin(); itr != whichlist.end(); ++itr)
		{
			cout << *itr << " ";
		}
		cout << endl;

	}


}